package com.heima.search.config;

import com.heima.search.service.ArticleSearchService;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: WDD
 * @Date: 2022/08/08/19:45
 * @Description:
 */
@Configuration
@EnableConfigurationProperties({EsConfigureOnProperties.class})
@ConditionalOnClass(ArticleSearchService.class)
public class EsClient {

    @Autowired
    private EsConfigureOnProperties esConfigureOnProperties;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(
                                esConfigureOnProperties.getHost(),
                                esConfigureOnProperties.getPort(),
                                "http"
                        )
                )
        );
    }
}
